//
//  ViewController.m
//  TestNumber
//
//  Created by Xuan Mai on 3/18/14.
//  Copyright (c) 2014 Xuan Mai. All rights reserved.
//

#import "ViewController.h"
#import "DataController.h"
#import "DBManager.h"
#import "ResultCell.h"
#import "Account.h"
#import "Global.h"
#import "AccountEntity.h"

#define COUNT_NUMBER 11
@interface ViewController (){
    UINib * cellLoader;
    NSMutableArray * arrAccount;
    
}

@end


@implementation ViewController
static NSString *CellClassName = @"ResultCell";

- (void)initData{
     cellLoader = [UINib nibWithNibName:CellClassName bundle:[NSBundle mainBundle]];
    arrAccount = [[NSMutableArray alloc] init];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    [self initData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)checkItClicked:(id)sender{
    [self.tfAccount resignFirstResponder];
    self.lblMessenger.text = [NSString stringWithFormat:@"The account number %@ must be an %i digit number",self.tfAccount.text,COUNT_NUMBER];
    self.lblMessenger.hidden = self.tfAccount.text.length == COUNT_NUMBER;
    if (!self.lblMessenger.hidden) {
        return;
    }
    [self addResult];
    

}

#pragma mark TEXTFILED DELEGATE

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:characterKeyInset] invertedSet];
    
    NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    
    return [string isEqualToString:filtered];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
#pragma mark - UITABLEVIEW DATASOURCE

- (void)addResult{
    AccountEntity * account = [[AccountEntity alloc] init];
    account.account = self.tfAccount.text;
    account.result = [DataController getResultWithAccount:self.tfAccount.text];
    NSLog(@"RESULT %@",account.result);
    account.attempted = [NSDate date];
    [DBManager InsertAccount:account];
    
    [arrAccount addObject:account];
    NSIndexPath *path0 = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray *indexPathArray = [NSArray arrayWithObjects:path0, nil];
    [self.tbResult insertRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationTop];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return arrAccount.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *cellIdentifier = @"Cell";
    ResultCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if(cell == nil)
    {
        cell = [[ResultCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        NSArray *topLevelItems = [cellLoader instantiateWithOwner:self options:nil];
        cell = [topLevelItems objectAtIndex:0];
        cell = (ResultCell *) [[[NSBundle mainBundle] loadNibNamed:CellClassName owner:self options:nil] lastObject];
    }
    
    AccountEntity * account = [arrAccount lastObject];
    cell.lblAccount.text = account.account;
    cell.lblResult.text = account.result;
    cell.lblAttempted.text = [DataController convertDateToString:account.attempted];
    return cell;
    
}

@end
